package com.gangye.controller;

import com.gangye.entity.City;
import com.gangye.service.CityService;
import com.gangye.tools.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Classname CityController
 * @Description city相关的控制路由
 * @Date 2020/5/27 10:11
 * @Created by gangye
 */
@RestController
@RequestMapping(value = "/cityController")
public class CityController {
    @Autowired
    private CityService cityService;

    //展示所有用户
    @PostMapping("/showAllCitys")
    public Response showAllCitys(){
        List<City> citys = cityService.showAllCitys();
        Response response = Response.newResponse();
        return response.setData(citys);
    }

    //根据id查询用户信息
    @PostMapping("/showOneCityInfo")
    public Response showOneCityInfo(Integer id){
        Response response = Response.newResponse();
        City cityInfo = cityService.getCityByPrimarykey(id);
        return response.setData(cityInfo);
    }

    //更改用户信息
    @PostMapping("/updateCityInfo")
    public Response updateCityInfo(@RequestBody City city){
        Response response = Response.newResponse();
        if (city.getId()!=null && ! "".equals(city.getId())){
            City tempCity = cityService.getCityByPrimarykey(city.getId());
            if (tempCity==null){
                return response.setCodeAndMessage(9999,"要更新的城市信息不存在！");
            }
            cityService.update(city);
            return response.OK();
        }
        return response.setCodeAndMessage(9999,"更改信息失败！");
    }

    //新增城市信息
    @PostMapping("/addCity")
    public Response addCity(@RequestBody City cityInfo){
        Response response = Response.newResponse();
        cityService.insertOneCity(cityInfo);
        return response.OK();
    }
}
